package com.rede.didiok.user.modulelist.rank.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.common.utils.PageUtils;
import com.rede.didiok.common.utils.Query;
import com.rede.didiok.user.modulelist.rank.dao.AttrGroupDao;
import com.rede.didiok.user.modulelist.rank.dao.RankCategoryDao;
import com.rede.didiok.user.modulelist.rank.entity.AttrEntity;
import com.rede.didiok.user.modulelist.rank.entity.AttrGroupEntity;
import com.rede.didiok.user.modulelist.rank.service.AttrGroupService;
import com.rede.didiok.user.modulelist.rank.service.AttrService;
import com.rede.didiok.user.modulelist.rank.vo.AttrGroupWithAttrsVo;
import com.rede.didiok.user.modulelist.rank.vo.RankMemberAttrGroupVo;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;
    @Resource
    private RankCategoryDao rankCategoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 按照榜单分类查找（分页）
     * @param params
     * @param catelogUid
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, String catelogUid) {
        // 检索条件多匹配
        String key = (String) params.getOrDefault("key", null);
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_uid",key).or().like("attr_group_name",key);
            });
        }

        if(StringUtils.isEmpty(catelogUid)){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("cat_uid",catelogUid);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    /**
     * 保存明细信息
     */
    @Override
    public void saveDetail(AttrGroupEntity attrGroup) {
        String catName = rankCategoryDao.getCatNameByCatId(attrGroup.getCatUid());
        attrGroup.setCatName(catName);
        this.save(attrGroup);
    }

    /**
     * 修改属性分组的明细信息
     * @param attrGroup
     */
    @Override
    public void updateDetail(AttrGroupEntity attrGroup) {
        String catName = rankCategoryDao.getCatNameByCatId(attrGroup.getCatUid());
        attrGroup.setCatName(catName);
        this.updateById(attrGroup);
    }

    /**
     * 根据分类id查出所有的分组以及分组关联的属性
     * @param catUid
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(String catUid) {
        // 1.根据分类Id查询分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("cat_uid", catUid));
        // 2.根据分组查询属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            // TODO 优化成批量查询
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查处当前memberId对应的所有属性分组信息 以及 当前分组下的所有属性对应的值
     *
     * @param memberUid
     * @param catUid
     * @return
     */
    @Override
    public List<RankMemberAttrGroupVo> getAttrGroupWithAttrsByMemberId(String memberUid, String catUid) {
        AttrGroupDao baseMapper = this.getBaseMapper();
        // 查处当前memberId对应的所有属性分组信息 以及 当前分组下的所有属性对应的值
        List<RankMemberAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(memberUid,catUid);
        return vos;
    }
}